//*
// 从地址中获取转发类型
//*
var fromeurl=document.referrer;
var addshareurl="https://www.pulu.com/webapi/api/PuluSharelist/Addpulu_share_q";//默认为问题
var insethelpurl="https://www.pulu.com/webapi/api/PuluAtme/AddAtMe_q";//默认为问题
if(/shareexppage/.test(fromeurl)){
    addshareurl="https://www.pulu.com/webapi/api/PuluSharelist/Addpulu_share_e";
    insethelpurl="https://www.pulu.com/webapi/api/PuluAtme/AddAtMe_e";
}else if(/mycountry|newsdetail/.test(fromeurl)){
    addshareurl="https://www.pulu.com/webapi/api/PuluSharelist/Addpulu_share";//新闻
}
var id= $.cookie("MYID");
var loca=location.href;
var type;//类型 1 2 3
var sending=false;//控制按钮点击
var newsNewShare;//新闻转发后的Id
if(loca.split("type=")[1]){
    type=loca.split("type=")[1].match(/\d+/)[0];
}
console.log(type);
var frienddata;//获取好友信息
if(type==="1"||type==="2"||type==="3"||type==="4"||type==="5"||type==="6"||type==="7"||type==="8"||type==="9"||type==="10"){
    //$("#callfriend").css("display","block");
    //不是新闻要获取好友列表
    $.ajax({
        datatype:"json",
        type:"get",
        url:"https://www.pulu.com/webapi/api/PuluGoodFriend/GetByUserId?userId="+id,
        success:function(d){
            console.log(d);
            if(d.Data.length>0){
                frienddata=d.Data;
                for(var i=0;i< d.Data.length;i++){
                    var headimg;
                    if(d.Data[i].headimg){
                        headimg='<img src="http://42.62.66.198:8003/'+ d.Data[i].headimg+'" height="40" width="40">'
                    }else{
                        headimg='<img src="img/af8.png" height="40" width="40">'
                    }
                    var realname="";
                    if(d.Data[i].realName){realname=" ("+d.Data[i].realName+")"}
                    $("#showfriendlist").append('<div class="card line-around module-user-small"><div class="layout-box media-graphic" onclick="choicefriend(this)" data-fid="'+ d.Data[i].friendId+'" data-fni="'+ d.Data[i].friendnick+'"><div class="mod-media size-xxs"><div class="media-main">'+headimg+'</div></div><div class="box-col item-list"><div class="item-main txt-m mct-a txt-cut">'+ d.Data[i].friendnick+realname+'</div></div></div></div>')
                }
            }
        }
    });
}
else{
    $("#callfriend").css("display","none");
}
//*
// 弹出表情
//*
var mySwiper;
$("#facebtn").click(function(){
    if($(".swiper-container").css("display")==="block"){
        $(".swiper-container").css("display","none");
        $("#keybordbtn").css("display","none");
        $("#facebtn").css("display","block");
        $("#contentword").focus();
    }else{
        $(".swiper-container").css("display","block");
        $("#keybordbtn").css("display","block");
        //swiper
        if(!mySwiper){
            mySwiper = new Swiper ('.swiper-container', {
                paginationClickable: true,
                touchRatio : 1,
                initialSlide :0,
                pagination: '.swiper-pagination'
            })
        }
    }
});

//*
// 打开好友列表
//*
$("#callfriend").click(function(){
    location.href+="#friendlist";
    $("#box").css("display","none");
    $("#friendlist").css("display","block");
});

//*
// 选择好友
//*
var choicefriendhash={};//判断有没有选择
var choicefriendarr=[];
var choicefriendnamearr=["@"];
function choicefriend(e){
    history.go(-1);
    console.log(e.dataset.fni);
    $("#box").css("display","block");
    $("#friendlist").css("display","none");
    console.log(choicefriendarr.length);
    if(choicefriendhash[e.dataset.fid]){
        //已经存在
        console.log("已经存在"+e.dataset.fid);
    }else{
        choicefriendarr.push(e.dataset.fid);
        choicefriendhash[e.dataset.fid]=1;
        choicefriendnamearr.push(e.dataset.fni);
    }
    $("#cancelsearch").css("display","none");
    $("#J-default").css("display","block");//好友列表页面
    $("#J-result").css("display","none");//好友搜索页面
    $("#searchfriendlist div").remove();//搜索的好友
    $("#searchfriendinput").val("")//清空搜索input
    console.log(choicefriendnamearr);
    var str=choicefriendnamearr.toString().replace(/,/,"");
    $("#callnamediv").html(str);
}

//*
//搜索好友
//*
$("#searchfriendinput").focus(function(){
    $("#cancelsearch").css("display","block");
    $("#J-default").css("display","none");
    $("#J-result").css("display","block");//好友搜索页面
    $("#searchfriendlist div").remove();//搜索的好友
});
//回车搜索
document.onkeydown=function(event){
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if(e && e.keyCode==13){ // enter 键
        if($("#J-result").css("display")==="block"){
            if($("#searchfriendinput").val().match(/^\s+$/g)){
                //console.log("内容为空");
            }else{
                $("#searchfriendlist div").remove();//
                var re = new RegExp($("#searchfriendinput").val().toLowerCase());
                for(var r=0;r<frienddata.length;r++){
                    if(re.test(frienddata[r].friendnick.toLowerCase())){
                        var headimg;
                        if(frienddata[r].headimg){
                            headimg='<img src="http://42.62.66.198:8003/'+ frienddata[r].headimg+'" height="40" width="40">'
                        }else{
                            headimg='<img src="img/af8.png" height="40" width="40">'
                        }
                        $("#searchfriendlist").append('<div class="card line-around module-user-small"><div class="layout-box media-graphic" onclick="choicefriend(this)" data-fid="'+ frienddata[r].friendId+'" data-fni="'+ frienddata[r].friendnick+'"><div class="mod-media size-xxs"><div class="media-main">'+headimg+'</div></div><div class="box-col item-list"><div class="item-main txt-m mct-a txt-cut">'+ frienddata[r].friendnick+'</div></div></div></div>');
                    }
                }
            }
        }
    }
};
//取消搜索
$("#cancelsearch").click(function(){
    $("#cancelsearch").css("display","none");
    $("#J-default").css("display","block");//好友列表页面
    $("#J-result").css("display","none");//好友搜索页面
    $("#searchfriendlist div").remove();//搜索的好友
    $("#searchfriendinput").val("")//清空搜索input
});

//*
//关闭好友列表
//*
$("#closefriend").click(function(){
    history.go(-1);
    $("#box").css("display","block");
    $("#friendlist").css("display","none");
});

//*
//手机按钮地址返回
//*
if( ("onhashchange" in window) && ((typeof document.documentMode==="undefined") || document.documentMode==8)) {
//浏览器支持onhashchange事件
    window.onhashchange = hashChangeFire;
    //对应新的hash执行的操作函数
} else { // 不支持则用定时器检测的办法
    setInterval(function() {
        var ischanged = isHashChanged(); //检测hash值或其中某一段是否更改的函数
        if(ischanged) {
            hashChangeFire(); //对应新的hash执行的操作函数
        }
    }, 150);
}
function hashChangeFire(){
    if(!/friendlist/.test(location.href)){
        //将好友列表关闭展示原页面
        $("#box").css("display","block");
        $("#friendlist").css("display","none");
    }
}

//*
//转发操作
//*
function goback(){
    $("#heightexpdiv").css("display","block");
    $("#sendwait").css("display","none");
}
//转发成功返回
$("#heightexpok").click(function(){
    //新闻转发 替换shareId
    if(/newsdetail.html/.test(document.referrer)){
        if($.cookie("newstypeid")){
            $.cookie("newsShareId",newsNewShare);
            history.go(-1);
        }else{
            //清除定位参数
            $.cookie("puluscrollTop","");
            $.cookie("pulucurrentIndex","");
            $.cookie("newsShareId",newsNewShare);
            history.go(-1);
            //location.href=document.referrer+"#newsShareId="+newsNewShare+"#fromuser="+id;
        }
    }else{
        history.go(-1);
    }
});
function shareftn(shareid,id,des,type){
    sending=true;//新闻正在转发
    $.ajax({
        type:"post",
        datatype:"json",
        url:addshareurl,
        data:{"contentid":shareid,"userid":id,"fromuser":"","des":des,"contenttype":type},
        success:function(data){
            newsNewShare=data.Data;
            //数据清除
            $.cookie("newsid","");
            $.cookie("queid","");
            $.cookie("expid","");
            $.cookie("sharenewsid","");
            //发送中提醒
            $("#sendwait").css("display","block");
            //页面返回
            setTimeout("goback()",500);
        }
    })
}
//*
//提醒谁看操作包含转发
//*
function callftn(shareid,id,choicefriendarr,des,type){
    console.log(insethelpurl);
    //先转发
    $.ajax({
        type:"post",
        datatype:"json",
        url:addshareurl,
        data:{"contentid":shareid,"userid":id,"fromuser":"","des":des,"contenttype":type},
        success:function(data){
            newsNewShare=data.Data;
            //再提醒谁看
            $.ajax({
                type:"post",
                datatype:"json",
                url:insethelpurl,
                data:{"questionId":shareid,"atmeuserId":id,"persons":choicefriendarr},
                success:function(d){
                    //数据清除
                    $.cookie("sharenewsid","");
                    $.cookie("sharequeid","");
                    $.cookie("shareexpid","");
                    //发送中提醒
                    $("#sendwait").css("display","block");
                    //页面返回
                    console.log("转发召唤成功");
                    setTimeout("goback()",500);
                }
            })
        }
    })
};
//新闻
function callftn2(shareid,id,choicefriendarr,des,type){
    console.log(insethelpurl);
    sending=true;//新闻正在转发
    //先转发
    $.ajax({
        type:"post",
        datatype:"json",
        url:addshareurl,
        data:{"contentid":shareid,"userid":id,"fromuser":"","des":des,"contenttype":type,"atUserIds":choicefriendarr},
        success:function(data){
            //数据清除
            $.cookie("sharenewsid","");
            $.cookie("sharequeid","");
            $.cookie("shareexpid","");
            //发送中提醒
            $("#sendwait").css("display","block");
            //页面返回
            setTimeout("goback()",500);
        }
    })
}
//*
//提醒谁看操作 发送按钮
//*
$("#sendbtn").click(function(){
    var des="";
    if($("#contentword").val().match(/^\s+$/g)){
        des="";
    }else{
        des=$("#contentword").val();
    }
    //新闻 只有转发
    if(type==="1"){
        var newsid=$.cookie("sharenewsid");
        //没有好友 转发
        if(choicefriendarr.length===0){
            if(!sending){
                shareftn(newsid,id,des,type);
            }
        }
        //有好友 提醒谁看
        else if(choicefriendarr.length>0){
            if(!sending){
                callftn2(newsid,id,choicefriendarr,des,type);
            }
        }
        //shareftn(newsid,id,des,type);
    }
    //问题
    else if(type==="2"){
        var queid=$.cookie("sharequeid");
        //没有好友 转发
        if(choicefriendarr.length===0){
            shareftn(queid,id,des,type);
        }
        //有好友 提醒谁看
        else if(choicefriendarr.length>0){
            callftn(queid,id,choicefriendarr,des,type);
        }
    }
    //经验
    else if(type==="3"||type==="4"||type==="5"||type==="6"||type==="7"||type==="8"||type==="9"||type==="10"){
        var expid=$.cookie("shareexpid");
        //没有好友 转发
        if(choicefriendarr.length===0){
            shareftn(expid,id,des,type);
        }
        //有好友 提醒谁看
        else if(choicefriendarr.length>0){
            callftn(expid,id,choicefriendarr,des,type)
        }
    }
});